package z_app;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.Serializable;
import java.awt.event.WindowStateListener;
import java.awt.event.WindowEvent;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.Color;

public class Watch extends JFrame {

	Calendar lastDay;
	SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
	SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
	String _fileName = "watch.dat";
	String saveDate = null; //수요일
	int nalsu=0; //제외할 날수
	
	private JPanel contentPane;
	private JLabel nowDate;
	private JLabel nowTime;
	private JLabel dday;
	private JMenuBar menuBar;
	private JMenu mnNewMenu;
	private JMenuItem mntmNewMenuItem;
	private JMenuItem mntmNewMenuItem_1;
	private JMenuItem menuItem;
	private JPanel panel;
	private JLabel lblNewLabel;
	private JLabel day;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Watch frame = new Watch();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Watch() {
		addComponentListener(new ComponentAdapter() {
			@Override
			public void componentResized(ComponentEvent arg0) {
				int w = (Watch.this).getWidth();
				int h = (Watch.this).getHeight();
				nowTime.setFont(new Font("굴림", Font.PLAIN, (w+h)/10));

			}
		});
		setTitle("수료 D-day");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 472, 252);
		setJMenuBar(getMenuBar_1());
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		contentPane.add(getNowDate(), BorderLayout.NORTH);
		contentPane.add(getNowTime(), BorderLayout.CENTER);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd (E요일)");
		Calendar c = Calendar.getInstance();
		String temp =sdf.format(c.getTimeInMillis());
		nowDate.setText(temp);
		contentPane.add(getDday(), BorderLayout.SOUTH);
		contentPane.add(getPanel(), BorderLayout.WEST);
		
		new InnerThread().start();
		
		fileLoad();
		
	}
	public void fileLoad(){
		File f = new File(_fileName);
		if(f.exists()){
			try{
				FileInputStream fis = new FileInputStream(f);
				ObjectInputStream ois = new ObjectInputStream(fis);
				WatchData wd = (WatchData)ois.readObject();
				
				saveDate = wd.getSaveDate();
				nalsu = wd.getNalsu();
				
				System.out.println(saveDate + "/" + nalsu);
				setLastDay();
				
				ois.close();
				fis.close();
				
				
			}catch(Exception ex){
				ex.printStackTrace();
			}
		}
		
	}
	
	public void setLastDay(){
		int y = Integer.parseInt(saveDate.substring(0,4));
		int m = Integer.parseInt(saveDate.substring(4,6));
		int d = Integer.parseInt(saveDate.substring(6,8));
		Calendar now = Calendar.getInstance();
		
		lastDay = Calendar.getInstance();
		lastDay.set(Calendar.YEAR, y);
		lastDay.set(Calendar.MONTH, m-1);
		lastDay.set(Calendar.DATE, d);
		
		long t1 = lastDay.getTimeInMillis() - now.getTimeInMillis();
		int t2 = (int)(t1/1000/60/60/24);
		
		dday.setText("수료일자 : " + sdf2.format(lastDay.getTimeInMillis()));
		day.setText("("+(t2-nalsu+1)+")");
	
	
//		Graphics g = dday.getGraphics();
//		g.drawString("str", 10,100);
		
		
		try{
			if(saveDate == null) return;
			FileOutputStream fos = new FileOutputStream(_fileName);
			
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			WatchData wd = new WatchData(saveDate, nalsu);
			
			oos.writeObject(wd);
			
			oos.close();			
			fos.close();
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		

	}

	private JLabel getNowDate() {
		if (nowDate == null) {
			nowDate = new JLabel("New label");
			nowDate.setForeground(Color.BLUE);
			nowDate.setHorizontalAlignment(SwingConstants.RIGHT);
			nowDate.setFont(new Font("굴림", Font.PLAIN, 30));
		}
		return nowDate;
	}
	private JLabel getNowTime() {
		if (nowTime == null) {
			nowTime = new JLabel("");
			nowTime.setHorizontalAlignment(SwingConstants.CENTER);
			nowTime.setFont(new Font("굴림", Font.PLAIN, 76));
			 
		}
		return nowTime;
	}
	
	
	private JLabel getDday() {
		if (dday == null) {
			dday = new JLabel("수료일을 설정하세요");
			dday.setForeground(Color.RED);
			dday.setFont(new Font("굴림", Font.PLAIN, 20));
			dday.setHorizontalAlignment(SwingConstants.RIGHT);
		}
		return dday;
	}
	private JMenuBar getMenuBar_1() {
		if (menuBar == null) {
			menuBar = new JMenuBar();
			menuBar.add(getMnNewMenu());
		}
		return menuBar;
	}
	private JMenu getMnNewMenu() {
		if (mnNewMenu == null) {
			mnNewMenu = new JMenu("설정");
			mnNewMenu.add(getMenuItem());
			mnNewMenu.add(getMntmNewMenuItem());
			mnNewMenu.add(getMntmNewMenuItem_1());
		}
		return mnNewMenu;
	}
	private JMenuItem getMntmNewMenuItem() {
		if (mntmNewMenuItem == null) {
			mntmNewMenuItem = new JMenuItem("수료일");
			mntmNewMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					String temp = JOptionPane.showInputDialog(Watch.this, 
							"수료일을 yyyymmdd 형식으로", saveDate);
					if(temp != null){
						saveDate = temp;
						setLastDay();
					}
				}
			});
		}
		return mntmNewMenuItem;
	}
	
	private JMenuItem getMntmNewMenuItem_1() {
		if (mntmNewMenuItem_1 == null) {
			mntmNewMenuItem_1 = new JMenuItem("제외할 일수");
			mntmNewMenuItem_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					String temp = JOptionPane.showInputDialog(Watch.this, "제외할 일수를 입력하세요", nalsu);
					if(temp != null){
						nalsu = Integer.parseInt(temp);
						setLastDay();
					}
				}
			});
		}
		return mntmNewMenuItem_1;
	}
	
	class InnerThread extends Thread{
		public void run(){
			while(true){
				
				Calendar c = Calendar.getInstance();
				String temp =sdf.format(c.getTimeInMillis());
				nowTime.setText(temp);
				try{
					Thread.sleep(1000);
				}catch(Exception ex){}
			}
		}
	}

	private JMenuItem getMenuItem() {
		if (menuItem == null) {
			menuItem = new JMenuItem("설정파일읽기");
			menuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					JFileChooser fc = new JFileChooser();
					int yn = fc.showOpenDialog(Watch.this);
					if(yn == JFileChooser.APPROVE_OPTION){
						_fileName = fc.getSelectedFile().getPath();
						fileLoad();
					}
					
				
				}
			});
		}
		return menuItem;
	}
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getLblNewLabel(), BorderLayout.NORTH);
			panel.add(getDay(), BorderLayout.CENTER);
		}
		return panel;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("남은일수");
			lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
			lblNewLabel.setForeground(Color.RED);
		}
		return lblNewLabel;
	}
	private JLabel getDay() {
		if (day == null) {
			day = new JLabel("00");
			day.setVerticalAlignment(SwingConstants.TOP);
			day.setForeground(Color.MAGENTA);
			day.setFont(new Font("굴림", Font.PLAIN, 70));
		}
		return day;
	}
}


class WatchData implements Serializable{
	String saveDate; // 수료일
	int nalsu; // 제외할 날수
	
	WatchData(String s, int n){
		this.saveDate = s;
		this.nalsu = n;
	}

	public String getSaveDate() {
		return saveDate;
	}

	public void setSaveDate(String saveDate) {
		this.saveDate = saveDate;
	}

	public int getNalsu() {
		return nalsu;
	}

	public void setNalsu(int nalsu) {
		this.nalsu = nalsu;
	}
	
}
